package com.cb.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.eduservice.entity.EduCourse;
import com.cb.eduservice.entity.EduTeacher;
import com.cb.eduservice.entity.vo.CourseInfoVo;
import com.cb.eduservice.entity.vo.CoursePublishVo;
import com.cb.eduservice.entity.vo.CourseQuery;
import com.cb.eduservice.service.EduCourseService;
import com.cb.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Cebb
 * @since 2020-06-22
 */
@Api(description = "课程管理")

@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    private final EduCourseService courseService;

    @Autowired
    public EduCourseController(EduCourseService courseService) {
        this.courseService = courseService;
    }

    @ApiOperation("添加课程基本信息")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(
            @ApiParam(name = "courseInfo", value = "课程基本信息", required = true)
            @RequestBody CourseInfoVo courseInfoVo) {
        String courseId = courseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("courseId", courseId);
    }

    @ApiOperation("根据课程id查询对应信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfoByCourseId(
            @ApiParam(name = "courseId", value = "课程id", required = true)
            @PathVariable String courseId) {
        CourseInfoVo courseInfo = courseService.getCourseInfoByCourseId(courseId);

        return R.ok().data("info", courseInfo);
    }

    @ApiOperation("根据ID修改课程信息")
    @PutMapping("{courseId}")
    public R updateCourseInfoByCourseId(
            @ApiParam(name = "courseId", value = "课程id", required = true)
            @PathVariable("courseId") String courseId,
            @ApiParam(name = "teacher", value = "修改后的课程信息", required = true)
            @RequestBody CourseInfoVo courseInfo) {

        courseService.updateCourseInfo(courseId, courseInfo);

        return R.ok().message("修改成功");
    }

    @ApiOperation("根据课程id查询最终发布信息")
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(
            @ApiParam(name = "courseId", value = "课程id", required = true)
            @PathVariable String courseId) {
        CoursePublishVo info = courseService.publishCourseInfo(courseId);

        return R.ok().data("info", info);
    }

    @ApiOperation("发布课程")
    @PutMapping("publish/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        boolean flag = courseService.updateById(eduCourse);
        if (!flag) {
            return R.error().message("发布失败..请稍等后尝试...");
        }

        return R.ok();
    }

    @ApiOperation("课程列表查询")
    @PostMapping("getCourseList/{current}/{limit}")
    public R getCourseList(@PathVariable("current") long current,
                           @PathVariable("limit") long limit,
                           @RequestBody CourseQuery courseQuery) {
        IPage<EduCourse> courseByPage = courseService.getCourseByPage(current, limit, courseQuery);
        long total = courseByPage.getTotal();
        List<EduCourse> records = courseByPage.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("根据课程id删除课程")
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        boolean flag = courseService.removeCourse(courseId);
        if (!flag) {
            return R.error().message("删除失败...");
        }

        return R.ok();
    }
}